<?php

namespace App\Admin\Controllers\Game;

use App\Admin\Constant\GameConst;
use App\Admin\Controllers\Controller;
use App\Admin\Filters\Game\GameGroupFilter;
use App\Admin\Models\Game\Game;
use App\Admin\Models\Game\GameGroup;
use App\Admin\Models\VueRouter;
use App\Admin\Requests\Game\GameGroupRequest;
use App\Admin\Resources\Game\GameGroupResource;
use App\Admin\Utils\Admin;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class GameGroupController extends Controller
{


    public function getGameList()
    {
//       $gamesData = DB::table('game_groups')->leftJoin('games','games.pid','=','game_groups.id')->select('game_groups.id','pid','games.name','game_groups.name as pName')->get();
        $gamesData = DB::table('games')->select('id', 'name', 'pid')->where(['is_delete'=>2])->get();
        $gameGroupsData = DB::table('game_groups')->select('name', 'id')->where(['is_delete'=>2])->get();
        $pushData = json_decode($gameGroupsData,true);

        $gamesData = json_decode($gamesData,true);

        $resetData = [];
        $s = false;


        foreach ($pushData as $key => $value) {
            $resetData[$key]['id'] = $value['id'];
            $resetData[$key]['label'] = $value['name'];
            $resetData[$key]['pid'] = 0;
            $resetData[$key]['yid'] = $value['id'];
            foreach ($gamesData as $k => $v) {
                if ($value['id'] == $v['pid']) {
                    $id= $v['id'];
                    $pid = $v['pid'];
                    $resetData[$key]['children'][] = [
                        'yid' => $id,
                        'id' => "$pid-$id",
                        'pid' => $pid,
                        'label' => $v['name'],
//                        'children'=>[]
                    ];
                }
            }
            if (!isset($resetData[$key]['children'])) {
                unset($resetData[$key]);
            }
        }
        $resetData = array_merge($resetData);

        return $this->ok($resetData);
    }


    public function getEditList()
    {
        $requestId = request()->input('id');
        $gameGroupData = GameGroup::query()->select('id','name')->where(['is_delete'=>2])->get();
        $gamesData = Game::query()->select('id', 'name', 'pid')->where(['is_delete'=>2])->get();
        $pushData = json_decode($gameGroupData,true);

        $gamesData = json_decode($gamesData,true);
        $resetData = [];
        $toData = [];
        foreach ($pushData as $key => $value) {
            if ($requestId == $value['id']) {
                $toData[$key]['id'] = $requestId;
                $toData[$key]['label'] = $value['name'];
                $toData[$key]['pid'] = 0;
                $toData[$key]['yid'] = $value['id'];
                foreach ($gamesData as $k => $v) {
                    if ($value['id'] == $v['pid']) {
                        $id= $v['id'];
                        $pid = $v['pid'];
                        $toData[$key]['children'][] = [
                            'yid' => $id,
                            'id' => "$pid-$id",
                            'pid' => $pid,
                            'label' => $v['name'],
//                        'children'=>[]
                        ];
                    }
                }
            } else {
                $resetData[$key]['id'] = $value['id'];
                $resetData[$key]['label'] = $value['name'];
                $resetData[$key]['pid'] = 0;
                $resetData[$key]['yid'] = $value['id'];
                foreach ($gamesData as $k => $v) {
                    if ($value['id'] == $v['pid']) {
                        $id= $v['id'];
                        $pid = $v['pid'];
                        $resetData[$key]['children'][] = [
                            'yid' => $id,
                            'id' => "$pid-$id",
                            'pid' => $pid,
                            'label' => $v['name'],
//                        'children'=>[]
                        ];
                    }
                }
            }

            if (!isset($resetData[$key]['children'])) {
                unset($resetData[$key]);
            }
            if (!isset($toData[$key]['children'])) {
                unset($toData[$key]);
            }
        }
        $resetData = array_merge($resetData);
        $toData = array_merge($toData);
        return $this->ok(['res' => $resetData, 'to' => $toData]);


    }
    public function getGroup(){

        $gameGroupData = GameGroup::query()->select('id','name')->where(['is_delete'=>2])->get();
        $cpData = DB::table('cp')->select('id','company_name')->get();
        return $this->ok(['res'=>$gameGroupData,'to'=>$cpData]);

    }

    public function index(GameGroupFilter $filter)
    {
        $gameGroups = GameGroup::query()
            ->filter($filter)
            ->where(['is_delete'=>2])
            ->paginate();

        return $this->ok(GameGroupResource::collection($gameGroups));
    }

    protected function formData($exceptRouterId = null)
    {
        $model = app(VueRouter::class);

        $vueRouters = $model->treeExcept($exceptRouterId ?? 0)->toTree();

        $roles = GameGroup::query()
            ->orderByDesc('id')
            ->get();

        $permissions = GameGroup::query()
            ->orderByDesc('id')
            ->get();

        return [
            'vue_routers' => $vueRouters,
            'roles' => $roles,
            'permissions' => $permissions,
        ];
    }

    public function create()
    {
        return $this->ok($this->formData());
    }

    public function store(GameGroupRequest $request)
    {


        $request->validated();
        $toData = $request->input('toData');
        $ids = [];
        foreach ($toData as $k => $v) {
            if (isset($v['children'])) {
                $ids = array_merge($ids, array_column($v['children'], 'yid'));
            }
        }
        $name = $request->input('name');
        $gameGroupName = GameGroup::query()->where(['name' => $name])->exists();
        if ($gameGroupName) {
            return response()->json(GameConst::GAME_GROUP_EXIST, 202, []);
        }
        $gameGroup = GameGroup::create(['name' => $name]);
        if (!empty($ids)) {
            Game::query()->whereIn('id', $ids)->update(['pid' => $gameGroup->id,'updated_at'=>time()]);
        }
        return response()->json(GameConst::GAME_GROUP__ADD_SUCCESS, 201, []);
    }

    public function edit(Request $request, GameGroup $gameGroup)
    {

        $toData = $request->input('toData');
        $name = $request->input('name');
        $ids = [];
        foreach ($toData as $k => $v) {
            if (isset($v['children'])) {
                $ids = array_merge($ids, array_column($v['children'], 'yid'));
            }
        }
        $id = GameGroup::query()->where(['name' => $name])->where(['is_delete'=>2])->value('id');
        if (!$id || empty($id)) {
            return response()->json(GameConst::GAME_GROUP_EDIT_FAIL, 203, []);
        }
        if (!empty($ids)) {
            Game::query()->whereIn('id', $ids)->update(['pid' => $id]);
        }
        return response()->json(GameConst::GAME_GROUP_EDIT_SUCCESS, 201, []);
    }

    public function update(GameGroupRequest $request, GameGroup $gameGroup)
    {
        $inputs = $request->validated();
        $gameGroup->update($inputs);

        return $this->created(GameGroupResource::make($gameGroup));
    }

    public function destroy($id)
    {

        GameGroup::query()->where('id',$id)->update(['is_delete'=>1,'delete_time'=>time()]);
        return $this->ok(GameConst::GAME_DELETE_SUCCESS);
    }
}
